*** Creates estimates for Table A1 and Figures 2 from the 1956-60 panel data

*** Creation of 56/60, 72/76, 92/96, and 00/04 data files documented in dataverse for 2017 AJPS - Polarization and the Decline of the American Floating Voter - dataverse link: https://doi.org/10.7910/DVN/4FCVUR

clear
use "data/amelia50s.dta" 
keep if pre56==1
merge 1:1 id using "data/observed50s.dta", keepusing(pref56 pref60 V560003) 
drop if _merge==2
drop _merge

svyset V560003

svy: proportion pid56
svy: proportion pid60

gen partyidswitch = 0 if pid56!=. & pid60!=.
replace partyidswitch = 1 if pid56<=2 & pid60>=4 & pid60<7
replace partyidswitch = 1 if pid56>=4 & pid56<7 & pid60<=2
svy: proportion partyidswitch

drop partyidswitch

gen prefswitch = 0 if pref56!=. & pref60!=.
replace prefswitch = 1 if pref56==1 & pref60==2
replace prefswitch = 1 if pref56==2 & pref60==1
svy: proportion prefswitch 
drop prefswitch

mi set mlong

misstable summarize

mi register imputed age catholic education  dempos56 demneg56 reppos56 repneg56 dempos58 demneg58 reppos58 repneg58 dempos60 demneg60 reppos60 repneg60 repcandpos56 repcandneg56 repcandpos60 repcandneg60 demcandpos56 demcandneg56 demcandpos60 demcandneg60 income56 income58 income60 prepref56 prepref60 pid60 pid58 pref56 pref60 

mi impute chained (truncreg, ll(21) ul(95) omit(i.pid58 i.pid60  income60 i.prepref56 i.prepref60 i.pref56 i.pref60 reppos* dempos* repneg* demneg* repcand* demcand*)) age (logit, omit(i.pid58 i.pid60 income60 i.prepref56 i.prepref60 i.pref56 i.pref60 reppos* dempos* repneg* demneg* repcand* demcand*)) catholic (ologit, ascontinuous omit(i.pid58 i.pid60 i.prepref56 i.prepref60 i.pref56 i.pref60 repcandpos56 repcandneg56 repcandpos60 repcandneg60 demcandpos56 demcandneg56 demcandpos60 demcandneg60 reppos58 repneg58 dempos58 demneg58 reppos60 repneg60 dempos60 demneg60)) education income56 income60 reppos56 dempos56 repneg56 demneg56  (ologit, ascontinuous omit(i.pid58 i.pid60 i.prepref60  i.pref56 i.pref60  reppos58 repneg58 dempos58 demneg58 reppos60 repneg60 dempos60 demneg60)) repcandpos56 repcandneg56 demcandpos56 demcandneg56 (mlogit, augment omit(i.pid58 i.pid60 income60 i.prepref60 i.pref60 reppos* dempos* repcandpos60 repcandneg60 demcandpos60 demcandneg60)) prepref56 pref56 (ologit, ascontinuous omit(i.pid56 i.pid60 i.prepref56 i.prepref60 i.pref56 i.pref60 repcand* demcand* reppos60 repneg60 dempos60 demneg60)) reppos58 repneg58 dempos58 demneg58 (ologit, omit(income60 i.prepref56 i.prepref60 i.pref60 repcand* demcand* reppos60 repneg60 dempos60 demneg60)) pid58 (ologit, ascontinuous omit(i.pid56 i.pid60 i.prepref56 i.prepref60 i.pref56 i.pref60 repcand* demcand*)) reppos60 repneg60 dempos60 demneg60 (ologit, omit(i.prepref60 i.prepref56 i.pref56 i.prepref60 i.pref60 repcand* demcand* reppos58 repneg58 dempos58 demneg58 i.pid56)) pid60  (ologit, ascontinuous omit(i.pid56 i.pid58 i.prepref56  i.pref56 i.pref60  reppos58 repneg58 dempos58 demneg58 reppos56 repneg56 dempos56 demneg56 i.prepref60 income56 repcandpos56 repcandneg56 demcandpos56 demcandneg56)) repcandpos60 repcandneg60 demcandpos60 demcandneg60 (mlogit, augment omit(i.prepref56 i.pref56 i.pref60 income56 i.pid56 i.pid58 repcandpos56 repcandneg56 demcandpos56 demcandneg56 reppos56 repneg56 dempos56 demneg56 reppos58 repneg58 dempos58 demneg58 reppos60 repneg60 dempos60 demneg60)) prepref60  (mlogit, augment omit(i.prepref56 i.pref56 income56 i.pid56 i.pid58 repcandpos56 repcandneg56 demcandpos56 demcandneg56 reppos56 repneg56 dempos56 demneg56 reppos58 repneg58 dempos58 demneg58 reppos60 repneg60 dempos60 demneg60)) pref60 = female south midwest black i.pid56, add(20) burnin(10) chaindots rseed(54321) 


mi estimate: svy: proportion pid56
mi estimate: svy: proportion pid60

mi passive: gen partyidswitch = 0 if pid56!=. & pid60!=.
mi passive: replace partyidswitch = 1 if pid56<=2 & pid60>=4 & pid60<7
mi passive: replace partyidswitch = 1 if pid56>=4 & pid56<7 & pid60<=2

mi estimate: svy: proportion partyidswitch

mi xeq: gen partyidswitchx = 0 
mi xeq: replace partyidswitchx = 1 if pid56<=2 & pid60>=4 & pid60<7
mi xeq: replace partyidswitchx = 1 if pid56>=4 & pid56<7 & pid60<=2

mi estimate: svy: proportion partyidswitch partyidswitchx

mi estimate: svy: proportion pref56 pref60

mi estimate, saving(mi_mlogit56, replace): mlogit pref56 i.pid56 age female black catholic south, base(1) 
mi predict xb56_1_mi using mi_mlogit56
mi predict xb56_2_mi using mi_mlogit56, equation(#2)
mi predict xb56_3_mi using mi_mlogit56, equation(#3)
mi xeq 0: summarize xb56_1_mi xb56_2_mi xb56_3_mi

mi passive: gen demvrepphat56 = 1/(1+exp(xb56_2_mi))
mi estimate: mean demvrepphat56
mi passive: gen simdem56 = uniform()<=demvrepphat56
mi passive: gen demvrepchoice56 = 0 if pref56<=2
mi passive: replace demvrepchoice56 = 1 if pref56==1
mi passive: replace demvrepchoice56 = simdem56 if demvrepchoice56==.

mi estimate: svy: proportion pref56 demvrepchoice56

mi estimate, saving(mi_mlogit60, replace): mlogit pref60 i.pid60 age female black catholic south, base(1) 

mi predict xb60_1_mi using mi_mlogit60
mi predict xb60_2_mi using mi_mlogit60, equation(#2)
mi predict xb60_3_mi using mi_mlogit60, equation(#3)
mi xeq 0: summarize xb60_1_mi xb60_2_mi xb60_3_mi

mi passive: gen demvrepphat60 = 1/(1+exp(xb60_2_mi))
mi estimate: mean demvrepphat60
mi passive: gen simdem60 = uniform()<=demvrepphat60
mi passive: gen demvrepchoice60 = 0 if pref60<=2
mi passive: replace demvrepchoice60 = 1 if pref60==1
mi passive: replace demvrepchoice60 = simdem60 if demvrepchoice60==.

mi estimate: svy: proportion pref60 demvrepchoice60

mi passive: gen supportswitch = 0 
mi passive: replace supportswitch = 1 if pref56==1 & pref60==2
mi passive: replace supportswitch = 1 if pref56==2 & pref60==1

mi estimate: svy: proportion supportswitch

drop supportswitch

****************
** no 3rd party
****************

** make sure imputed values are consistent

mi passive: gen supportswitch = 0 
mi passive: replace supportswitch = 1 if demvrepchoice56!=demvrepchoice60
mi estimate: svy: proportion supportswitch 
